package com.own.stu.apm.agent.core.plugin;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public enum PluginCfg {
    INSTANCE;

    private List<PluginDefineItem> pluginclassList = new ArrayList<>();

    public List<PluginDefineItem> getPluginClassList() {
        return pluginclassList;
    }

    void load(InputStream input) throws IOException {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
            String pluginDefine;
            while ((pluginDefine = reader.readLine()) != null) {
                try {
                    if (pluginDefine.trim().length() == 0 || pluginDefine.startsWith("#")) {
                        continue;
                    }
                    PluginDefineItem plugin = PluginDefineItem.build(pluginDefine);
                    pluginclassList.add(plugin);
                } catch (Exception e) {
                    log.error("Failed to format plugin({}) define.", pluginDefine, e);
                }
            }
        }
    }
}
